<%--
  Created by IntelliJ IDEA.
  User: 15433
  Date: 2019/7/13
  Time: 11:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel="stylesheet" type="text/css" href="js/login.css">
</head>
<body onload="initBody()">
<%
    // 获取浏览器发送过来的cookie, 获取用户信息
    Cookie[] cookies = request.getCookies();
    String username = "";
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if ("username".equals(cookie.getName())) {
                username = cookie.getValue();
            }
        }
    }
%>

<div id="content">
    <div class="login-header">
        <img src="Image/logo.png">
    </div>
    <form action="LoginServlet" method="post">
        <div class="login-input-box">
            <span class="icon icon-user"></span>
            <input  type="text" name="Account" value="<%= username%>"

                    placeholder="Please enter your username"

            >
        </div>
        <div class="login-input-box">
            <span class="icon icon-password"></span>

            <input  type="password" name="Password"

                    placeholder="Please enter your password"
            >
        </div>
        <div class="captcha" >
            <span class="icon "></span>
            <input input type="text" name="CAPTCHA">
            <img id="verify">
            <input  type="button" value="看不清? 换一张" id="btn">
        </div>

    <div class="login-button-box">
        <button   type="submit" value="登录" onclick="
    if(Account.value===''){
    alert('用户名不能为空');
    return false;
}else if(Password.value===''){
    alert('密码不能为空');
    return false;
}else if(CAPTCHA.value===''){
    alert('验证码不能为空');
    return false;
}" >Login</button>

    </div>
    </form>
    <div class="logon-box">
        <a href="register.jsp">Register</a>
    </div>
</div>
<span style="color: red" class="mes">
    <%
        if(request.getAttribute("error")!=null){
    %>
        用户名或密码错误
    <%
        }
        if(request.getAttribute("verifyerror")!=null){
    %>
        验证码错误
    <%
        }
    %>
</span>
<script type="text/javascript">

    document.getElementById("btn").onclick = function () {
        // 获取img元素
        // 为了让浏览器发送请求到servlet, 所以一定要改变src
        changeImg();
    };

    function initBody() {
        changeImg();
    }

    function changeImg() {
        document.getElementById("verify").src =
            "./CaptchaServlet?time=" + new Date().getTime();
    }
</script>

</body>
</html>
